package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.smedi.ismedi.constructionproject.api.dto.request.ProjectAssignmentListQueryRequest;
import com.smedi.ismedi.constructionproject.api.dto.response.ProjectBasicResponse;
import com.smedi.ismedi.constructionproject.api.dto.response.resp.ProjectAssignmentResp;
import com.smedi.ismedi.constructionproject.api.feign.RemoteConstructionProjectFeignApi;
import com.smedi.ismedi.constructionproject.api.feign.RemoteProjectAssignmentFeignApi;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.announcement.Receiver;
import com.smedi.ismedi.inmail.core.domain.dto.ProjectAssignmentListQuery;
import com.smedi.ismedi.inmail.core.infrastructure.constant.AnnouncementError;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.service.ProjectAssignmentRemoteApiService;
import com.smedi.ismedi.personnel.api.dto.response.PersonSimpleResponse;
import com.smedi.ismedi.personnel.api.feign.RemotePersonCoreFeignApi;
import com.smedi.ismedi.personnel.api.feign.RemotePersonValidateFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 远程项目接口实现类
 *
 * @author jiangqinxuan
 * @date 2021/07/27
 */
@Service
@AllArgsConstructor
public class ProjectAssignmentRemoteApiServiceImpl implements ProjectAssignmentRemoteApiService {
    private final RemoteProjectAssignmentFeignApi remoteProjectAssignmentFeignApi;
    private final RemoteConstructionProjectFeignApi remoteConstructionProjectFeignApi;
    private final RemotePersonValidateFeignApi remotePersonValidateFeignApi;
    private final RemotePersonCoreFeignApi remotePersonCoreFeignApi;
    private final AdapterConverter adapterConverter;

    @Override
    public List<Long> queryReceiverIdListByOrganizationIdList(List<Long> organizationIdList, Boolean internal) {
        List<Long> receiverIdListByOrganizationList = new ArrayList<>();
        List<Long> receiverIdListTemp = new ArrayList<>();

        for (Long organizationIdTemp : organizationIdList) {
            //根据组织id找到其所属人员作为接收者
            List<PersonSimpleResponse> personSimpleResponseList = remotePersonCoreFeignApi.getQueryPersonSimpleByCurrentHrOrganizationId(organizationIdTemp).getData();
            if (!personSimpleResponseList.isEmpty()) {
                for (PersonSimpleResponse personSimpleResponseTemp : personSimpleResponseList) {
                    Long receiverIdListByOrganizationIdTemp = personSimpleResponseTemp.getPersonId();
                    receiverIdListByOrganizationList.add(receiverIdListByOrganizationIdTemp);
                }

                // 如果是限内部，只保留企业人员
                if (internal) {
                    for (Long receiverIdListByOrganizationIdTemp : receiverIdListByOrganizationList) {
                        if (remotePersonValidateFeignApi.getCheckPersonIsInternal(receiverIdListByOrganizationIdTemp).getData()) {
                            receiverIdListTemp.add(receiverIdListByOrganizationIdTemp);
                        }
                    }
                } else {
                    receiverIdListTemp.addAll(receiverIdListByOrganizationList);
                }
            }
        }
        // 对查询到的接收者列表去重
        HashSet<Long> hashSet = new HashSet<>(receiverIdListTemp);
        receiverIdListTemp.clear();
        receiverIdListTemp.addAll(hashSet);
        //receiverIdList.addAll(receiverIdListTemp);

        return receiverIdListTemp;
    }

    @Override
    public List<Receiver> queryReceiverByOrganizationIdList(List<Long> organizationIdList) {
        //把主体院的接收者构建出receiver
        List<Receiver> receiverOrganizationList = new ArrayList<>();

        for (Long receiverIdTemp : organizationIdList) {
            Receiver receiverTemp1 = new Receiver();
            receiverOrganizationList.add(receiverTemp1);
        }

        return receiverOrganizationList;
    }

    @Override
    public List<ProjectBasicResponse> queryAllProjectList() {
        ResponseData<List<ProjectBasicResponse>> responseData = remoteConstructionProjectFeignApi.getQueryAvailableProjectBasicList();
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return responseData.getData();
    }

    @Override
    public List<Receiver> queryReceiverIdListByProjectIdList(List<Long> projectIdList, List<Long> receiverIdList, List<Receiver> receiverOrganizationList, Boolean internal) {
        List<Receiver> receiverList = new ArrayList<>();
        return receiverList;
    }

    @Override
    public List<ProjectAssignmentResp> getProjectAssignmentList(ProjectAssignmentListQuery projectAssignmentListQuery) {
        ProjectAssignmentListQueryRequest request  = adapterConverter.queryToRequest(projectAssignmentListQuery);
        ResponseData<List<ProjectAssignmentResp>> responseData = remoteProjectAssignmentFeignApi.getProjectAssignmentList(request);
        if(!responseData.isSuccess()){
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return responseData.getData();
    }
}
